package model

import (
	"gorm.io/gorm"
	__ "video-srv/basic/proto/rpc"
)

type Video struct {
	gorm.Model
	UserId   int    `gorm:"type:int" json:"userId"`
	Title    string `gorm:"type:varchar(30)" json:"title"`
	Category string `gorm:"type:varchar(30)" json:"category"`
	Tags     string `gorm:"type:varchar(30)" json:"tags"`
	CoverUrl string `gorm:"type:varchar(255)" json:"coverUrl"`
	VideoUrl string `gorm:"type:varchar(255)" json:"videoUrl"`
	Status   int    `gorm:"type:int" json:"status"`
}

type CheckVideo struct {
	gorm.Model
	UserId  int    `gorm:"type:int" json:"userId"`
	VideoId int    `gorm:"type:int" json:"videoId"`
	Status  int    `gorm:"type:int" json:"status"`
	Reason  string `gorm:"type:varchar(100)" json:"reason"`
}

func (v *Video) PublishVideo(db *gorm.DB) error {
	return db.Create(&v).Error
}

func (c *CheckVideo) CheckVideo(db *gorm.DB) error {
	return db.Create(&c).Error
}

func (v *Video) UpdateVideoStatus(db *gorm.DB, videoId, status int) error {
	db.Model(&v).Where("id = ?", videoId).Update("status", status)
	err := db.Where("id = ?", videoId).First(&v).Error
	return err
}

func (v *Video) GetVideoListById(db *gorm.DB, videoId int) (info *__.GetVideoListByIdResp, err error) {
	err = db.Model(&v).
		Select("videos.user_id,title,category,tags,cover_url,video_url,check_videos.`status`,check_videos.created_at,check_videos.reason").
		Joins("LEFT JOIN check_videos ON  check_videos.video_id = videos.id").
		Where("videos.id = ?", videoId).First(&info).Error

	return
}
